package com.tdk.bts.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DruidConnectionUtil {
	
	 static DataSource dataSource;
	
	static {
		Properties pro = new Properties();
		//根据配置文件，生成properties对象的值
		InputStream in = DruidConnectionUtil.class.getClassLoader().getResourceAsStream("druid.properties");
		try {
			pro.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//
		try {
			dataSource = DruidDataSourceFactory.createDataSource(pro);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static JdbcTemplate getJDBCTemplate() {
		return new JdbcTemplate(dataSource);
	}

	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public static void closeAll( Statement st, ResultSet rs) throws SQLException {

		if (st != null) {
			st.close();
		}
		if (rs != null) {
			rs.close();
		}
	}
	public static void closeAll( PreparedStatement st, ResultSet rs) throws SQLException {
		if (st != null) {
			st.close();
		}
		if (rs != null) {
			rs.close();
		}
	}
	
	
}
